/*
* CourseCollectDaoImpl.java
* Created on  2014-5-15 下午4:49
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-5-15       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.course.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.ptas.course.dao.CourseCollectDao;
import com.genghis.ptas.course.entity.CourseCollect;
import com.genghis.ptas.course.entity.CourseInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 类的描述信息
 *
 * @author gaoxinyu
 * @version 1.0.1
 */
@Repository("courseCollectDao")
public class CourseCollectDaoImpl extends PageDao implements CourseCollectDao {

    @Override
    public int addCourseCollectService(CourseCollect courseCollect) {
        String sql = "INSERT INTO course_collect(course_id, user_id, collect_date) " +
                "VALUES(:courseId, :userId, :collectDate)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(courseCollect), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public List<CourseInfo> findCourseCollectByUserId(Integer userId) {
        String sql = "SELECT course.* FROM course_collect cc LEFT JOIN course ON cc.`course_id` = course.id WHERE cc.user_id = ?";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<CourseInfo>(CourseInfo.class), userId);
    }

    @Override
    public List<CourseCollect> findCourseCollectByUserIdAndCourseId(Integer userId, Integer courseId) {
        String sql = "SELECT cc.* FROM course_collect cc LEFT JOIN course ON cc.`course_id` = course.id " +
                "WHERE cc.user_id = ? AND cc.course_id = " + courseId;
        return getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<CourseCollect>(CourseCollect.class), userId);
    }
}
